<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends CommonController
{
    var $product;
    var $advert;
    var $category;
    var $gallery;

    function __construct()
    {
        parent::__construct();
        $this->product = M('Product');
        $this->advert = M('Advert');
        $this->category = M('Category');
        $this->gallery = M('Gallery');
    }

    /***
     * 加载首页
     */

    public function index()
    {
        //查出全部
        $this->alls = $this->product->where("status = 1")->order("sort_order")->
        limit(4)->select();

        //查出新品
        $this->news = $this->product->where("status = 1 AND is_new =1")->order("sort_order")->
        limit(2)->select();
        //查出热销
        $this->hots = $this->product->where("status = 1 AND is_hot=1")->order("sort_order")->
        limit(2)->select();
        //查出推荐
        $this->recommends = $this->product->where("status = 1 AND is_recommend=1")->order("sort_order")->
        limit(2)->select();


        $this->display();
    }

    /***
     * 全部商品
     */
    public function all()
    {
        $count = $this->product->where("status = 1")->count();
        $products = $this->product->where("status = 1")->order("sort_order")
            ->limit(2)->select();
        $this->assign(compact('count','products'));
        $this->display();
    }
    public function data(){
        $start = I('post.start');
//        $id = I('post.id');
        $list = $this->product->where('status=1')->order('sort_order')->limit($start,2)->select();
        $this->ajaxReturn(array( 'result'=>$list,'status'=>1, 'msg'=>'获取成功！'));
    }

    /***
     * 全部分类
     */
    public function category()
    {

        $categories = $this->category->where("parent_id != 0")->select();
        foreach ($categories as &$category) {
            $category['children'] = $this->product->where("category_id ='$category[id]' and status = 1")->select();
        }

        $this->assign(compact('categories'));
        $this->display();
    }

    /***
     * 展示
     */
    public function show()
    {
        $id = I('get.id');
        $products = $this->product->where("id = '$id'")->find();

        $galleries = $this->gallery->where("product_id = '$id'")->select();

        $this->assign(compact('products', 'galleries'));
        $this->display();
    }
}